package sys

import (
	"context"
	"github.com/google/wire"
	"sieve_admin_server/app/api/util"
	"sieve_admin_server/app/contextx"
	"sieve_admin_server/app/server"
	serviceSys "sieve_admin_server/app/service/sys"
)

var (
	MenuSet = wire.NewSet(wire.Struct(new(MenuAPI), "*"))
)

type MenuAPI struct {
	util.EmptyAPI
	MenuSrv *serviceSys.MenuSrv
}

func (a *MenuAPI) GetApis() []*server.BatchModTree {
	return []*server.BatchModTree{
		{
			Obj:   a,
			Name:  "menu",
			Title: "菜单管理", Perms: []*server.Perm{
				{Name: "queryMenu", Title: "查询菜单"},
				{Name: "queryMenuDetail", Title: "查询菜单"},
			},
			Order:       8,
			DisableMenu: 1,
		},
	}
}

func (a *MenuAPI) API_queryMenu_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("level", true), // 1:顶级菜单 2:二级菜单
			},
		)
	)

	return a.MenuSrv.QueryMenu(context.Background(), reqArgs)
}

func (a *MenuAPI) AddMenus(ctx context.Context, node []*server.BatchModTree) interface{} {

	return a.MenuSrv.InitMenusWrap(ctx, node)
}

func (a *MenuAPI) API_queryMenuDetail_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("role_id", true),
			},
		)
	)

	var (
		id       = uint64(ctx.Uid)
		isRoleId bool
	)
	if v, ok := reqArgs["role_id"]; ok {
		isRoleId = true
		id = v.(uint64)
	}

	return a.MenuSrv.GetMenus(context.Background(), id, isRoleId)
}
